package cn.mxh.pethspt.dao.impl;

import cn.mxh.pethspt.dao.JiuZhenDao;
import cn.mxh.pethspt.entity.Ding;
import cn.mxh.pethspt.entity.JiuZhen;
import cn.mxh.pethspt.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

public class JiuZhenDaoImpl implements JiuZhenDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 添加就诊信息
     *
     * @param jiuZhen
     * @return
     */
    @Override
    public int add(JiuZhen jiuZhen) throws Exception {
        int num = 0;
        try{
            String sql = "INSERT into jiuZhen(jTime,pId,coId,mId,mCount,sId,bId,uId,eId,jstatus,dingId)VALUES(?,?,?,?,?,?,?,?,?,?,?)";
            Object[] objects = new Object[]{
                    jiuZhen.getjTime(),
                    jiuZhen.getpId(),
                    jiuZhen.getCoId(),
                    jiuZhen.getmId(),
                    jiuZhen.getmCount(),
                    jiuZhen.getsId(),
                    jiuZhen.getbId(),
                    jiuZhen.getuId(),
                    jiuZhen.geteId(),
                    jiuZhen.getJstatus(),
                    jiuZhen.getDingId()

            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 跟据id修改就诊信息
     *
     * @param jiuzhen
     * @return
     */
    @Override
    public int update(JiuZhen jiuzhen) throws Exception {
        int num = 0;
        try{
            String sql = "update jiuzhen set jstatus = ? where jId = ?;";
            Object[] objects = new Object[]{
                    jiuzhen.getJstatus(),
                    jiuzhen.getjId()
            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有就诊信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<JiuZhen> getAll() throws Exception {
        List<JiuZhen> jiuZhenList = new ArrayList<JiuZhen>();
        try{
            String sql = "select * from jiuzhen;";
            jiuZhenList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<JiuZhen>(JiuZhen.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return jiuZhenList;
    }

    /**
     * 根据就诊id查询主人id
     *
     * @param jid
     * @return
     * @throws Exception
     */
    @Override
    public JiuZhen getJiuZhenByjid(Integer jid) throws Exception {
        JiuZhen jiuZhen = new JiuZhen();
        try{
            String sql = "select uId from jiuzhen where jId = ?;";
            jiuZhen = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<JiuZhen>(JiuZhen.class),jid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return jiuZhen;
    }
}
